<template>
  <!-- 分配管理权限 -->
  <el-dialog title="修改管理权限" @close="close" :visible.sync="dialogVisible" width="35%">

    <!-- 主体 -->
    <el-form ref="form" label-width="80px">
      <el-form-item label="当前的用户名:" label-width="100">
        <span v-if="SetUsersRolesData">{{SetUsersRolesData.username}}</span>
      </el-form-item>
      <el-form-item label="当前管理权限:" label-width="100">
        <span v-if="SetUsersRolesData">{{SetUsersRolesData.role_name}}</span>
      </el-form-item>
      <el-form-item label="分配新的权限:" label-width="100">
        <el-select v-model="SelectvalueId" placeholder="请选择">
          <el-option v-for="item in RolesList" :key="item.id" :label="item.roleName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <!-- 按钮 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="btnPutroles">确 定</el-button>
    </span>

  </el-dialog>
</template>

<script>
export default {
  name: 'SetUsersRoles',
  data() {
    return {
      dialogVisible: this.diaSetUsersRoles, //弹窗开关
      RolesList: null,   //管理员列表
      SelectvalueId: null,   //选择器选中的值
    }
  },
  props: {
    diaSetUsersRoles: Boolean,  //父级弹窗开关
    SetUsersRolesData: Object
  },
  watch: {
    diaSetUsersRoles() {  //监听父级弹窗开关
      this.dialogVisible = this.diaSetUsersRoles
      this.getRoles()
    }
  },
  methods: {
    close() {  //关闭弹窗调用
      this.$emit('OutdiaSetRoles')
      this.SelectvalueId = ''
    },
    getRoles() {  //获取管理员列表
      this.$http.get('roles').then((res) => {
        if (res.data.meta.status !== 200) return
        this.RolesList = res.data.data
      })
    },
    btnPutroles() {  //按钮修改管理权限
      if (!this.SelectvalueId) return this.$mess('请选择新分配权限', 'error')
      this.$http.put(`users/${this.SetUsersRolesData.id}/role`, { rid: this.SelectvalueId }).then(res => {
        if (res.data.meta.status !== 200) return this.$mess(res.data.meta.msg, 'error')
        this.$mess(res.data.meta.msg, 'success')
        this.$emit('getUser')
         this.$emit('OutdiaSetRoles')
      })
    }
  }
}
</script>

<style>
</style>